package main.java.jdbc2;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * @Auther:Liu
 * @Testname:jdbc2
 * @Date:2025/3/12 9:54
 */
public class jdbc2 {
    public static void main(String[] args) throws Exception {
        jdbc2 jdbc2 = new jdbc2();

//        jdbc2.isAdmin();
//        jdbc2.findall1();
//        jdbc2.insert();
//        jdbc2.addsql();
//        jdbc2.delete();

//        {ArrayList<user> findall = jdbc2.findall();
//        ArrayList<user> users = new ArrayList<user>();
//        for (int i = 0; i < findall.size() ; i++) {
//            System.out.println(findall.get(i).getId()+"_|_"+findall.get(i).getAdmire()+"_|_"+findall.get(i).getPassword());
//        }}//ArraysList遍历
    }

    //链接
    public Connection conn() throws Exception {
        String url = "jdbc:mysql://localhost:3306/student?useSSL=false&serverTimezone=UTC";
        String user = "root";
        String password = "123456";
        Connection conn = DriverManager.getConnection(url, user, password);
        return conn;
    }

    //登录
    public void isAdmin() throws Exception {
        Connection conn = conn();
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名:");
        String name = scanner.next();
        System.out.println("请输入密码:");
        String pas = scanner.next();
        String sql = "select * from manage where admire=? and password=?";
        PreparedStatement preparedStatement = conn.prepareStatement(sql);
        preparedStatement.setString(1, name);
        preparedStatement.setString(2, pas);
        ResultSet resultSet = preparedStatement.executeQuery();
        if (resultSet.next()) {
            System.out.println("登陆成功");
        } else {
            System.out.println("登录失败");
        }
    }

    //查询(AeeaysList遍历)
    public ArrayList<user> findall() throws Exception {
        ArrayList<user> users = new ArrayList<user>();
        Connection conn = conn();
        String sql = "select * from manage ";
        PreparedStatement preparedStatement = conn.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            String admire1 = resultSet.getString("admire");
            String password1 = resultSet.getString("password");
            int id1 = resultSet.getInt("id");
                user user = new user();
                user.setId(String.valueOf(id1));
                user.setAdmire(admire1);
                user.setPassword(password1);
                users.add(user);
        }
        return users;
    }

    //查询(ordinary)
    public void findall1() throws Exception {
        Connection conn = conn();
        String sql = "select * from manage ";
        PreparedStatement preparedStatement = conn.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            String admire1 = resultSet.getString("admire");
            String password1 = resultSet.getString("password");
            int id1 = resultSet.getInt("id");
            System.out.println(id1 + "||" + admire1 + "||" + password1);
        }
        preparedStatement.close();
        resultSet.close();
    }

    //插入
    public void insert() throws Exception {
        Connection conn = conn();
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要插入的用户名:");
        String name = scanner.next();
        System.out.println("请输入要插入的密码:");
        String pas = scanner.next();
        System.out.println("请输入要插入的id:");
        String id = scanner.next();
        String sql = "insert into manage values(?,?,?)";
        PreparedStatement preparedStatement = conn.prepareStatement(sql);
        preparedStatement.setString(1, name);
        preparedStatement.setString(2, pas);
        preparedStatement.setString(3, id);
        int i = preparedStatement.executeUpdate();
        if (i != 0) {
            System.out.println("插入成功");
        } else {
            System.out.println("插入失败");
        }
    }

    //更新
    public void addsql() throws Exception {
        Connection conn = conn();
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要更改的用户名:");
        String name = scanner.next();
        System.out.println("请输入要更改的密码:");
        String pas = scanner.next();
        System.out.println("请输入要更改的id:");
        int id = scanner.nextInt();
        String sql = "update  manage set admire=?,password=?  where id=?";
        PreparedStatement preparedStatement = conn.prepareStatement(sql);
        preparedStatement.setString(1, name);
        preparedStatement.setString(2, pas);
        preparedStatement.setInt(3, id);
        int i = preparedStatement.executeUpdate();
        if (i != 0) {
            System.out.println("更改成功");
        } else {
            System.out.println("更改失败");
        }
    }

    //删除
    public void delete() throws Exception {
        Connection conn = conn();
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要删除的id:");
        String id = scanner.next();
        String sql = "delete from  manage where id=?";
        PreparedStatement preparedStatement = conn.prepareStatement(sql);
        preparedStatement.setString(1, id);
        int i = preparedStatement.executeUpdate();
        if (i != 0) {
            System.out.println("删除成功");
        } else {
            System.out.println("删除失败");
        }
    }
}
